﻿using System;
using System.Collections.Generic;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using System.Collections;
using NHibernate;
using System.Text.RegularExpressions;


namespace CMS.Data
{
    public class DonThuVBCDDao : AbstractNHibernateDao<DonThuVBCD, DonThuVBCD.DomainObjectID>, IDonThuVBCDDao
    {
        #region Create Instance
        private static DonThuVBCDDao instance = null;
        public static DonThuVBCDDao Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DonThuVBCDDao();
                }
                return instance;
            }
        }
        #endregion

        /// <summary>
        /// Xóa các văn bản chỉ đạo liên quan đến đơn thư
        /// </summary>
        /// <param name="DonThuID"></param>
        /// <returns></returns>
        public bool DeleteByDonThu(string DonThuID)
        {
            try
            {
                String strQuery = @"From DonThuVBCD o Where o.ID.MaDonThu =:MaDonThu";
                List<DonThuVBCD> list = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                        .SetParameter("MaDonThu", DonThuID)
                        .List<DonThuVBCD>() as List<DonThuVBCD>;
                DonThuVBCD obj;
                DonThuVBCD.DomainObjectID id; 
                foreach (DonThuVBCD item in list)
	            {
		            id = new DonThuVBCD.DomainObjectID(DonThuID, item.ID.MaVanBan);
                    obj = this.GetById(id);
                    this.Delete(obj);
	            } 
                return true;
            }
            catch
            {
                return false;
            }
        }

        public IList GetDonThuWithVanBan(int MaVanBan)
        {
            IList results = new ArrayList(); ;
            try
            {
                String strQuery = @"From DonThuVBCD o Where o.ID.MaVanBan =:MaVanBan";
                results = NHibernateSessionManager.Instance.GetSession().CreateQuery(strQuery)
                        .SetParameter("MaVanBan", MaVanBan)
                        .List();

                return results;
            }
            catch (Exception ex)
            {
                System.Console.Write(ex);
                return results;
            }
        }
    }
}
